# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=cocos2d-x-console
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=r470.037a13f
pkgrel=1
pkgdesc="cocos2d command line tool (mingw-w64)"
arch=('any')
url="http://www.cocos2d-x.org/"
license=("MIT")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-cocos2d-x"
         "${MINGW_PACKAGE_PREFIX}-cmake")
makedepends=('git')

options=('staticlibs' 'strip')
source=(#"${_realname}"::"git+https://github.com/martell/cocos2d-console.git#branch=msys2")
        "${_realname}"::"git+https://github.com/cocos2d/cocos2d-console.git")
sha256sums=('SKIP')

pkgver() {
  cd "$srcdir/$_realname"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "$srcdir/$_realname"

  #patch the location of cococs2d.ini
  #sed -i "s|os.path.join(cocos2d_path, \"cocos2d.ini\")|\"${MINGW_PREFIX}/share/cocos2d-x/console/cocos2d.ini\"|g" $srcdir/$_realname/bin/cocos.py
  sed -i "s|~/.cocos2d-js/cocos2d.ini|${MINGW_PREFIX}/share/cocos2d-x/console/cocos2d.ini|g" $srcdir/$_realname/bin/cocos.py

  #we use python2 not python ;)
  sed -i "s|exec python|exec /usr/bin/python2|g" $srcdir/$_realname/bin/cocos
  sed -i "s|#!/usr/bin/python|#!/usr/bin/python2|g" $srcdir/$_realname/bin/cocos.py

  #maybe i should just create a new cocos script
  #fixup cocos
  sed -i "s|exec python|exec /usr/bin/python2|g" $srcdir/$_realname/bin/cocos
  sed -i "s|\$COCOS_CONSOLE_BIN_DIRECTORY/cocos.py|${MINGW_PREFIX}/bin/cocos.py|g" $srcdir/$_realname/bin/cocos

}

build() {
  cd "$srcdir/$_realname"

  #set the location of the templates
  sed -i "s|templates=|templates=${MINGW_PREFIX}/share/cocos2d-x/templates|g" $srcdir/$_realname/bin/cocos2d.ini
  sed -i "s|plugins=../plugins|plugins=${MINGW_PREFIX}/share/cocos2d-x/console/plugins|g" $srcdir/$_realname/bin/cocos2d.ini

}

package() {
  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"

  cp -r $srcdir/$_realname/bin/cocos.py ${pkgdir}${MINGW_PREFIX}/bin
  cp -r $srcdir/$_realname/bin/cocos ${pkgdir}${MINGW_PREFIX}/bin
  cp -r $srcdir/$_realname/bin/cocos_project.py ${pkgdir}${MINGW_PREFIX}/bin

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/cocos2d-x/console"
  cp -r $srcdir/$_realname/bin/cocos2d.ini ${pkgdir}${MINGW_PREFIX}/share/cocos2d-x/console
  cp -r $srcdir/$_realname/plugins/ ${pkgdir}${MINGW_PREFIX}/share/cocos2d-x/console/

}
